package com.zhonghui.common.eshopService.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eshop.service.Impl.BaseServiceImpl;
import com.zhonghui.common.eshopDomain.StoreBargainUser;
import com.zhonghui.common.eshopMapper.StoreBargainUserMapper;
import com.zhonghui.common.eshopService.StoreBargainUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StoreBargainUserServiceImpl  extends BaseServiceImpl<StoreBargainUserMapper, StoreBargainUser> implements StoreBargainUserService {

    @Autowired
    private StoreBargainUserMapper storeBargainUserMapper;

    public void setBargainUserStatus(Long bargainId, Long uid) {
        StoreBargainUser storeBargainUser = this.getBargainUserInfo(bargainId, uid);
        if (!ObjectUtil.isNull(storeBargainUser)) {
            if (storeBargainUser.getStatus() == 1) {
                double price = NumberUtil.sub(NumberUtil.sub(storeBargainUser.getBargainPrice(), storeBargainUser.getBargainPriceMin()), storeBargainUser.getPrice()).doubleValue();
                if (price <= 0.0D) {
                    storeBargainUser.setStatus(3);
                    this.storeBargainUserMapper.updateById(storeBargainUser);
                }
            }
        }
    }

    public StoreBargainUser getBargainUserInfo(Long bargainId, Long uid) {
        LambdaQueryWrapper<StoreBargainUser> wrapper = new LambdaQueryWrapper();
        wrapper.eq(StoreBargainUser::getBargainId, bargainId).eq(StoreBargainUser::getUid, uid).last("limit 1");
        return (StoreBargainUser)this.storeBargainUserMapper.selectOne(wrapper);
    }
}
